package cn.tedu._02notice.controller;

import cn.tedu._02notice.base.response.JsonResult;
import cn.tedu._02notice.base.response.StatusCode;
import cn.tedu._02notice.mapper.NoticeMapper;
import cn.tedu._02notice.pojo.dto.NoticeAddParam;
import cn.tedu._02notice.pojo.dto.NoticeListQuery;
import cn.tedu._02notice.pojo.dto.NoticeUpdateParam;
import cn.tedu._02notice.pojo.entiy.Notice;
import cn.tedu._02notice.pojo.vo.NoticeDetailInfoVO;
import cn.tedu._02notice.pojo.vo.NoticeListVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;


@Slf4j
@RestController
@Tag(name ="01-资讯管理接口")
@RequestMapping("/v1/notice")
public class NoticeController {
    @Autowired
    private NoticeMapper noticeMapper;

    @PostMapping("add")
    @Operation(summary = "资讯添加")
    @ApiOperationSupport(order = 10)
    @Parameters(value = {@Parameter(name = "title",description = "资讯标题",required = true,example="天天跑酷"),
    @Parameter(name="content",description = "资讯内容",required = true,example="《天天跑酷》是周杰伦的专辑"),
            @Parameter(name="type",description = "资讯类型",required = true,example="1"),
            @Parameter(name="status",description = "资讯状态",required = true,example="1"),
    @Parameter(name="noticeAddParam",hidden = true)})
     public JsonResult insertNotice(NoticeAddParam noticeAddParam){
        log.debug("添加公告："+noticeAddParam);
        Notice notice = new Notice();
        BeanUtils.copyProperties(noticeAddParam,notice);

        notice.setUserId(1L);
        notice.setCreateTime(new Date());
        notice.setUpdateTime(new Date());
        int rows = noticeMapper.insert(notice);
        if(rows>0) {
            return JsonResult.ok();
        }else{
            return new JsonResult(StatusCode.OPERATION_FAILED);
        }
    }

    @GetMapping("list")
    @Operation(summary = "查询资讯")
    @ApiOperationSupport(order = 20)
    @Parameters(value = {@Parameter(name = "title",description = "资讯标题",required = true,example="天天跑酷"),
    @Parameter(name="type",description = "资讯类型",required = true,example="1"),
    @Parameter(name="noticeListQuery",hidden = true)})
    public JsonResult listNotice(NoticeListQuery noticeListQuery){
        log.debug("查询公告："+noticeListQuery);
        return  JsonResult.ok(noticeMapper.selectByTitleAndType(noticeListQuery));
    }

    @GetMapping("detail/{id}")
    @Operation(summary = "查询资讯详情")
    @ApiOperationSupport(order = 30)
    @Parameters(value = {@Parameter(name = "id",description = "资讯id",required = true,example="1"),
    @Parameter(name ="name",description = "资讯名称",required = true,example="天天跑酷",hidden=true),})
    public JsonResult detailNotice(@PathVariable Long id,String name){
        log.debug("查询公告详情："+id);
        return JsonResult.ok(noticeMapper.selectById(id));
    }

    @DeleteMapping("del")
    @Operation(summary = "删除资讯")
    @ApiOperationSupport(order = 40)
    public JsonResult deleteNotice(Long id){
        log.debug("删除公告："+id);
        noticeMapper.deleteById(id);
        return JsonResult.ok();
    }

    @PostMapping("update")
    @Operation(summary = "更新资讯")
    @ApiOperationSupport(order = 50)
    public JsonResult updateNotice(@RequestBody NoticeUpdateParam noticeUpdateParam){
        log.debug("修改公告："+noticeUpdateParam);
        Notice notice = new Notice();
        BeanUtils.copyProperties(noticeUpdateParam,notice);
        notice.setUpdateTime(new Date());
        int rows = noticeMapper.updateById(notice);
        if(rows>0) {
            return JsonResult.ok();
        }else{
            return new JsonResult(StatusCode.OPERATION_FAILED);
        }
    }
}
